<?php
namespace Common\Model;
use Think\Model;
class ArticleModel extends Model{
	public $pagesize = 10;
	
	public function info($article_id){
		$where['id'] = $article_id;
		$info = $this->where($where)->find();
		$info = $this->filter_field($info);
		$info['tags_list'] = D('Tags')->get_tags_list('article_' . $info['module'], $article_id);
		return $info;
	}
	/**
	 * 获取文章列表
	 * @param  array   $where    where条件
	 * @param  string  $order    排序
	 * @param  integer $page     第几页
	 * @param  integer $pagesize 分页数量
	 */
	public function get_list($where, $order = '', $page = 1, $pagesize = 0){
		$pagesize = $pagesize ? $pagesize : $this->pagesize;
		$first_row = $pagesize * ($page - 1);

		//默认排序
		if(!$order){
			$order['order_id']  = 'asc';
			$order['edit_time'] = 'desc';
			$order['id']        = 'desc';
		}

		$where['status']    = 1;
		$where['is_show']   = 1;
		$where['is_delete'] = 0;
		
		$count = $this->where($where)->count();
		$list  = $this->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			$new_list[] = $this->filter_field($rs);
		}
		$this->count = $count;
		return $new_list ? $new_list : array();
	}
	/**
	 * 相关文章列表
	 * @param  string  $module  模块标识
	 * @param  string  $keyword 关键字
	 * @param  integer $num     显示数量
	 */
	public function related_article_list($module = '', $keyword = '', $article_id = '', $num = 10){
		if(!$keyword){
			return array();
		}
		if($article_id){
			$where['id'] = array('neq', $article_id);
		}
		$keyword_list = explode(' ', $keyword);
		foreach ($keyword_list as $kw) {
			$where_kw[] = array('like', '%' . $kw . '%');
		}
		$where_kw[] = 'or';
		$where['title']  = $where_kw;
		$where['module'] = $module;
		$list = $this->get_list($where, '', 1, $num);

		return $list;
	}
	public function filter_field($data){
		$data['article_id']      = $data['id'];
		$data['date_desc']       = date('Y-m-d', $data['edit_time']);
		$data['time_desc']       = time_ago_desc($data['edit_time']);
		$data['datetime_desc']   = date('Y-m-d H:i:s', $data['edit_time']);
		$data['cover_url']       = D('File')->get_path($data['cover_id']);
		$data['cover_file_info'] = D('File')->get_file_list($data['cover_id']);
		$data['category_name']   = D('Category')->get_name($data['category_id']);
		$data['status_desc']     = $status_desc[$data['status']];
		$data['content']         = $this->get_content($data['article_id']);

		unset($data['id']);
		return $data;
	}
	public function update_num($article_id = '', $field = 'fav_num', $type = 'add'){
		if(!$article_id){
			return false;
		}
		$where['id'] = $article_id;
		if($type == 'add'){
			$this->where($where)->setInc($field);
		}
		if($type == 'minus'){
			$this->where($where)->setDec($field);
		}
	}
	/**
	 * 获取文章内容
	 * @param  string $article_id 文章ID
	 */
	public function get_content($article_id = ''){
		$where['article_id'] = $article_id;
		$content = D('ArticleContent')->where($where)->getField('content');
		return $content ? $content : '';
	}
	/**
	 * 获取单篇文章内容
	 * @param  [type] $module [description]
	 * @return [type]         [description]
	 */
	public function get_page_content($module, $field = 'content'){
		$where['module'] = $module;
		$info = D('Page')->where($where)->find();

		return $info ? ($field ? $info[$field] : $info) : array();
	}
	/**
	 * 更新查看次数
	 * @param  [type] $article_id 文章ID
	 * @param  [type] $user_id    用户ID
	 * @param  [type] $session_id SessionID
	 */
	public function update_view_num($article_id = 0, $user_id = 0, $session_id = ''){
		if(!$article_id || !$session_id){
			return false;
		}
		$session_id = md5($session_id);
		if($user_id){
			$where['user_id']    = $user_id;
		}else{
			$where['session_id'] = $session_id;
		}
		$where['article_id'] = $article_id;
		if(D('ArticleVisitLogs')->where($where)->count()){
			return false;
		}

		$data['session_id']  = $session_id;
		$data['user_id']     = $user_id ? $user_id : 0;
		$data['article_id']  = $article_id;
		$data['ip']          = get_client_ip();
		$data['create_time'] = time();
		D('ArticleVisitLogs')->add($data);

		unset($where);
		$where['id'] = $article_id;
		$this->where($where)->setInc('view_num');

		return true;
	}
	
}


